import visitMonthPlanModel from "../model/visitMonthPlanModel";
import visitMonthPlanModel2 from "../model2/visitMonthPlanModel2";

import addExportModel from '../model/addExportModel';
import addExportModel2 from '../model2/addExportModel2';

import approvalModel from "../model/approvalModel";
import approvalModel2 from "../model2/approvalModel2";

import consumerRecoderModel from "../model/consumerRecoderModel";
import consumerRecoderModel2 from "../model2/consumerRecoderModel2";

import storeRecoderModel from "../model/storeRecoderModel";
import storeRecoderModel2 from "../model2/storeRecoderModel2";

import excelExportModel from '../model/excelExportModel';
import excelExportModel2 from '../model2/excelExportModel2';


import storeInfoModel from "../model/storeInfoModel";
import storeInfoModel2 from "../model2/storeInfoModel2";

import userCheckInModel from "../model/userCheckInModel";
import userCheckInModel2 from "../model2/userCheckInModel2";

import * as moment from 'moment';
//备份数据 Service
class BackupService{
    constructor(){}

    //备份月拜访计划和拜访记录
    async backUpPlanAndRecoder(){
        console.log('开始!!!!!!!!!!!!',moment().format('YYYY-MM-DD HH:mm:ss'))
        let plan_data=await visitMonthPlanModel.find({});
        let storeArr:any[]=[]   //已经备份的门店记录id列表
        let conArr:any[]=[]   //已经备份的客户记录id列表
        if(plan_data.length>0){
            for (let pindex = 0; pindex < plan_data.length; pindex++) {
                let p = plan_data[pindex];
                //p:员工月拜访计划
                //if(pindex==0){
                    let b=await visitMonthPlanModel2.create({
                        sourceId: p.sourceId,
                        userId: p.userId,
                        user:p.user,
                        avatar:p.avatar,
                        jobnumber: p.jobnumber,
                        deptId: p.deptId,
                        provinceManager: p.provinceManager,
                        areaManager: p.areaManager,
                        modelManager: p.modelManager,      //模块负责人
                        nationalManager: p.nationalManager,//全国负责人
                        city: p.city,
                        roles: p.roles,     //角色
                        province: p.province,//员工所在省份
                        area: p.area,//员工所在区域
                        manageModel: p.manageModel,     //员工所在模块
                        team: p.team,//员工所在团队
                        nation:p.nation,
                        provinceName: p.provinceName,//省份名称
                        areaName: p.areaName,//区域名称
                        manageModelName: p.manageModelName,//模块名称
                        teamName:p.teamName,//团队名称
                        nationName: p.nationName,//全国名称
                        month: p.month,
                        visitRecoder: p.visitRecoder,
                        channel: p.channel,
                        mark: p.mark,
                        planDate: p.planDate,
                        year:p.year,
                        consumerName: p.consumerName,
                        consumerCode: p.consumerCode,
                        rest: p.rest,
                        meeting: p.meeting,
                        plantype: p.plantype,
                        planStatus: p.planStatus,
                        customerType:p.customerType,
                        purpose: p.purpose,
                        comment: p.comment,
                        cancelApply: p.cancelApply,
                        remark: p.remark,
                        leadIndate:p.leadIndate
                    })
                    //console.log(b._id,'<<year新的计划id<<<<<<<<<<<<',b.planDate,b.userId,'===',p.visitRecoder==b.visitRecoder)
                    let re_id=p.visitRecoder;//拜访记录id
                    let type=p.plantype
                    console.log(re_id,'_re_id',pindex)
                    if(p&&re_id){
                        if(type=='门店'){
                            console.log('门店')
                            let re_=await storeRecoderModel.findById({_id:re_id});
                            if(re_){
                                let b2=await storeRecoderModel2.create({
                                    sourceId: re_.sourceId,
                                    planId: b._id,
                                    timestamp: re_.timestamp,
                                    userid: re_.userid,//员工的userid
                                    month: re_.month,
                                    date: re_.date,
                                    planDate: re_.planDate,
                                    planType: re_.planType,
                                    storeName: re_.storeName,
                                    storeCode: re_.storeCode,
                                    storeType: re_.storeType,//示范网吧  模范店等
                                    position: re_.position,
                                    isChuchai: re_.isChuchai,//是否出差
                                    isListSample: re_.isListSample,
                                    sampleList: re_.sampleList,
                                    isListStandard: re_.isListStandard,
                                    competitor: re_.competitor,
                                    storeActivity: re_.storeActivity,
                                    pictureStoreActivity: re_.pictureStoreActivity,
                                    picture: re_.picture,//门头照片
                                    isListLetter: re_.isListLetter,
                                    listLetter: re_.listLetter,
                                    information: re_.information
                                                                    
                                })
                                let a2=await visitMonthPlanModel2.updateOne({_id:b._id},{visitRecoder:b2._id})
                                console.log(b.planDate,a2,'更新月计划中拜访记录的id==visitRecoder',b2._id,'<<<<<新的拜访记录id<<<<<<<<<')
                                storeArr.push(re_._id)
                            }else{
                                let a2=await visitMonthPlanModel2.updateOne({_id:b._id},{visitRecoder:''})
                                console.log('拜访记录为空!!!',p.visitRecoder,b.planDate,a2,'更新月计划中拜访记录的id==visitRecoder')
                            }
                        }else{
                            console.log('客户!!!')
                            let a=await consumerRecoderModel.findById({_id:re_id});
                            if(a){
                                let b2=await consumerRecoderModel2.create({
                                    sourceId: a.sourceId,
                                    planId: b._id,
                                    year:a.year,
                                    userid:a.userid,
                                    jobnumber: a.jobnumber,
                                    userName: a.userName,
                                    
                                    consumerName: a.consumerName,
                                    consumerCode: a.consumerCode,
                                    city: a.city,
                                    cityType: a.cityType,
                                    typeOne: a.typeOne,
                                    typeTwo:a.typeTwo,
                                    typeThree: a.typeThree,
                                    typeFour: a.typeFour,
                                    
                                    typeFive: a.typeFive,
                                    typeSix: a.typeSix,
                                    typeSeven: a.typeSeven,
                                    typeEight: a.typeEight,
                                    typeNine: a.typeNine,
                                    typeTen: a.typeTen,
                                    comment:a.comment,
                                    timestamp: a.timestamp,
                                    month: a.month,
                                    date:a.date,
                                    planDate:a.planDate,
                                    planType: a.planType,
                                    consumerType: a.consumerType,
                                    vistitWay: a.vistitWay,
                                    position: a.position,
                                    isChuchai: a.isChuchai,
                                    picture: a.picture,
                                    purpose:a.purpose,
                                    talkingPerson:a.talkingPerson,
                                    job: a.job,
                                    connection: a.connection,
                                    visitResult: a.visitResult,
                                    followMatter: a.followMatter,
                                    cooperation: a.cooperation 
                                                                    
                                })
                                let a2=await visitMonthPlanModel2.updateOne({_id:b._id},{visitRecoder:b2._id})
                                console.log(b.planDate,a2,'更新月计划中拜访记录的id==visitRecoder',b2._id,'<<<<<新的拜访记录id<<<<<<<<<')
                                conArr.push(a._id)
                            }else{
                                let a2=await visitMonthPlanModel2.updateOne({_id:b._id},{visitRecoder:''})
                                console.log('拜访记录为空!!!',p.visitRecoder,b.planDate,a2,'更新月计划中拜访记录的id==visitRecoder')
                            }
                        }
                    }
                //}
            }
        }
        console.log(storeArr.length,'<<<<门店id,客户id>>>>',conArr.length)
        let arr2=await consumerRecoderModel.distinct('_id',{comment:2});
        let arr3=await consumerRecoderModel2.distinct('_id',{comment:2})
        console.log(arr2.length,'原来的记录id列表长度',arr3.length,'备份后的记录id列表长度')
        //客户拜访记录
        let moreArr=await consumerRecoderModel.distinct('_id',{_id:{$nin:conArr},comment:2});
        console.log(moreArr.length,'没有备份的客户拜访记录列表')
        for (let mindex = 0; mindex < moreArr.length; mindex++) {
            let id = moreArr[mindex];
            let m=await consumerRecoderModel.findById({_id:id})
            let b2=await consumerRecoderModel2.create({
                sourceId: m.sourceId,
                planId: m._id,
                year:m.year,
                userid:m.userid,
                jobnumber: m.jobnumber,
                userName: m.userName,
                
                consumerName: m.consumerName,
                consumerCode: m.consumerCode,
                city: m.city,
                cityType: m.cityType,
                typeOne: m.typeOne,
                typeTwo:m.typeTwo,
                typeThree: m.typeThree,
                typeFour: m.typeFour,
                
                typeFive: m.typeFive,
                typeSix: m.typeSix,
                typeSeven: m.typeSeven,
                typeEight: m.typeEight,
                typeNine: m.typeNine,
                typeTen: m.typeTen,
                comment:m.comment,
                timestamp: m.timestamp,
                month: m.month,
                date:m.date,
                planDate:m.planDate,
                planType: m.planType,
                consumerType: m.consumerType,
                vistitWay: m.vistitWay,
                position: m.position,
                isChuchai: m.isChuchai,
                picture: m.picture,
                purpose:m.purpose,
                talkingPerson:m.talkingPerson,
                job: m.job,
                connection: m.connection,
                visitResult: m.visitResult,
                followMatter: m.followMatter,
                cooperation: m.cooperation 
                                                
            })
            console.log(b2._id,'备份剩余的客户拜访记录!!!!!',mindex)
        }
       
        let moreArr2=await storeRecoderModel.distinct('_id',{_id:{$nin:storeArr}});
        console.log(moreArr.length,'没有备份的门店拜访记录列表')
        for (let mindex = 0; mindex < moreArr2.length; mindex++) {
            let id = moreArr2[mindex];
            let re_=await storeRecoderModel.findById({_id:id})
            let b2=await storeRecoderModel2.create({
                sourceId: re_.sourceId,
                planId:re_.planId,
                timestamp: re_.timestamp,
                userid: re_.userid,//员工的userid
                month: re_.month,
                date: re_.date,
                planDate: re_.planDate,
                planType: re_.planType,
                storeName: re_.storeName,
                storeCode: re_.storeCode,
                storeType: re_.storeType,//示范网吧  模范店等
                position: re_.position,
                isChuchai: re_.isChuchai,//是否出差
                isListSample: re_.isListSample,
                sampleList: re_.sampleList,
                isListStandard: re_.isListStandard,
                competitor: re_.competitor,
                storeActivity: re_.storeActivity,
                pictureStoreActivity: re_.pictureStoreActivity,
                picture: re_.picture,//门头照片
                isListLetter: re_.isListLetter,
                listLetter: re_.listLetter,
                information: re_.information
            })
            console.log(b2._id,'备份剩余的门店拜访记录!!!!!',mindex)
        }
        console.log('结束!!!!!!!!!!!!',moment().format('YYYY-MM-DD HH:mm:ss'))
       

      
        

        
    }


    async backupMonthPlan(){
        let data=await visitMonthPlanModel.find({})
        let data2=await visitMonthPlanModel2.find({})
        console.log(data.length,'????????????',data2.length)
        /* 
        if(data.length>0){
            for (let index = 0; index < data.length; index++) {
                let element = data[index];
                let b=await visitMonthPlanModel2.create({
                    sourceId: element.sourceId,
                    userId: element.userId,
                    user:element.user,
                    avatar:element.avatar,
                    jobnumber: element.jobnumber,
                    deptId: element.deptId,
                    provinceManager: element.provinceManager,
                    areaManager: element.areaManager,
                    modelManager: element.modelManager,      //模块负责人
                    nationalManager: element.nationalManager,//全国负责人
                    city: element.city,
                    roles: element.roles,     //角色
                    province: element.province,//员工所在省份
                    area: element.area,//员工所在区域
                    manageModel: element.manageModel,     //员工所在模块
                    team: element.team,//员工所在团队
                    nation:element.nation,
                    provinceName: element.provinceName,//省份名称
                    areaName: element.areaName,//区域名称
                    manageModelName: element.manageModelName,//模块名称
                    teamName:element.teamName,//团队名称
                    nationName: element.nationName,//全国名称
                    month: element.month,
                    visitRecoder: element.visitRecoder,
                    channel: element.channel,
                    mark: element.mark,
                    planDate: element.planDate,
                    consumerName: element.consumerName,
                    consumerCode: element.consumerCode,
                    rest: element.rest,
                    meeting: element.meeting,
                    plantype: element.plantype,
                    planStatus: element.planStatus,
                    customerType:element.customerType,
                    purpose: element.purpose,
                    comment: element.comment,
                    cancelApply: element.cancelApply,
                    remark: element.remark,
                    leadIndate:element.leadIndate
                })
                console.log(b._id,'<<<<<<<<<<<<<<',b.planDate,b.userId)
            }
        }  */
        console.log('添加结束!!!!!!!!!')
    }

    async backupaddEXport(){
        let data=await addExportModel.find({})
        let data2=await addExportModel2.find({})
        console.log(data.length,'????????????',data2.length)
       
        if(data.length>0){
            for (let index = 0; index < data.length; index++) {
                let a = data[index];
                let b=await addExportModel2.create({
                    name_id:a.name_id,
                    name_:a.name_,
                    date_:a.date_,
                    month:a.month,
                    consumerCount_day:a.consumerCount_day,//日均
                    consumerCount_empl:a.consumerCount_empl,//人均
                    comment:a.comment
                })
                console.log(b,'<<<<<<<<<<<<<<',)
            }
        }   
        console.log('添加结束!!!!!!!!!')



    }



    async backupApproval2(){
        let data=await approvalModel.find({})
        let data2=await approvalModel2.find({})
        console.log(data.length,'????????????',data2.length)
        /* 
        if(data.length>0){
            for (let index = 0; index < data.length; index++) {
                let a = data[index];
                let b=await addExportModel2.create({
                    name_id:a.name_id,
                    name_:a.name_,
                    date_:a.date_,
                    month:a.month,
                    consumerCount_day:a.consumerCount_day,//日均
                    consumerCount_empl:a.consumerCount_empl,//人均
                    comment:a.comment
                })
                console.log(b,'<<<<<<<<<<<<<<',)
            }
        }   */
        console.log('添加结束!!!!!!!!!')



    }

    
    async backupVisitConsumerRecoder2(){
        let data=await consumerRecoderModel.find({comment:1})
        let data2=await consumerRecoderModel2.find({})
        console.log(data.length,'????????????',data2.length)
     
        if(data.length>0){
            for (let index = 0; index < data.length; index++) {
                let a = data[index];
                let b=await consumerRecoderModel2.create({
                    sourceId: a.sourceId,
                    planId: a.planId,
                  
                    userid:a.userid,
                    jobnumber: a.jobnumber,
                    userName: a.userName,
                    
                    consumerName: a.consumerName,
                    consumerCode: a.consumerCode,
                    city: a.city,
                    cityType: a.cityType,
                    typeOne: a.typeOne,
                    typeTwo:a.typeTwo,
                    typeThree: a.typeThree,
                    typeFour: a.typeFour,
                    
                    typeFive: a.typeFive,
                    typeSix: a.typeSix,
                    typeSeven: a.typeSeven,
                    typeEight: a.typeEight,
                    typeNine: a.typeNine,
                    typeTen: a.typeTen,

                    comment:a.comment,

                    timestamp: a.timestamp,
                    month: a.month,
                    date:a.date,
                    planDate:a.planDate,
                    planType: a.planType,
                    consumerType: a.consumerType,
                    vistitWay: a.vistitWay,
                    position: a.position,
                    isChuchai: a.isChuchai,
                    picture: a.picture,
                    purpose:a.purpose,
                    talkingPerson:a.talkingPerson,
                    job: a.job,
                    connection: a.connection,
                    visitResult: a.visitResult,
                    followMatter: a.followMatter,
                   
                    cooperation: a.cooperation 
                })
                console.log(b,'<<<<<<<<<<<<<<',index)
            }
        }  
        console.log('添加结束!!!!!!!!!')



    }


    async backupExcelExport2(){
        let data=await excelExportModel.find({})
        let data2=await excelExportModel2.find({})
        console.log(data.length,'????????????',data2.length)
      /* 
        if(data.length>0){
            for (let index = 0; index < data.length; index++) {
                let a = data[index];
                let b=await excelExportModel2.create({
                     userId:a.userId,
                    userName:a.userName,
                    date_:a.date_,
                    visitCount_plan:a.visitCount_plan,//计划拜访客户数量(一天)
                    actuall_visitCount:a.actuall_visitCount,//实际拜访数量
                    consumerRate:a.consumerRate, //客户拜访达成率(一天)

                    newConsumerRate:a.newConsumerRate, //新客户拜访达成率
                    newConsumerCount_plan:a.newConsumerCount_plan,//计划拜访新客户数量
                    newConsumerCount_visit:a.newConsumerCount_visit,//实际拜访新客户数量

                    checkInRate:a.checkInRate,//签到率
                    actuall_checkIn:a.actuall_checkIn,//实际签到天数
                    checkInTime:a.checkInTime,

                    unBase_check:a.unBase_check,

                    reachStandardRate:a.reachStandardRate,//已拜访门店达标率
                    reachStandardStoreCount:a.reachStandardStoreCount,//门店达标的数量

                    visitedStoreCount:a.visitedStoreCount,//已经拜访的门店数量

                    storeSchadule:a.storeSchadule,//门店拜访进度
                    totalCountStore:a.totalCountStore,//员工名下总的门店数量


                    year:a.year,
                    month: a.month,
                    comment:a.comment
                })
                console.log(b,'<<<<<<<<<<<<<<',index)
            }
        }    */ 
        console.log('添加结束!!!!!!!!!')



    }


    async backupStoreInfo2(){
        let data=await storeInfoModel.find({})
        let data2=await storeInfoModel2.find({})
        console.log(data.length,'????????????',data2.length)
         
        if(data.length>0){
            for (let index = 0; index < data.length; index++) {
                let bill = data[index];
                let b=await storeInfoModel2.create({
                    sourceId: bill.sourceId,
                    storeCode: bill.storeCode,//门店编码
                    storeName: bill.storeName,//门店名称
                    store_classification: bill.store_classification,//门店类型
                    channel: bill.channel,//渠道
                    contact_name: bill.contact_name,//联系人姓名
                    phone: bill.phone,//电话
                    consumerCode: bill.consumerCode,//客户编码
                    consumerName: bill.consumerName,//客户名称
                    city: bill.city,//城市
                    cityType: bill.cityType,//城市级别
                    store_address: bill.store_address,//门店地址
                    iT_Mall_name: bill.iT_Mall_name,//IT Mall name
                    iT_Mall_address: bill.iT_Mall_address,//IT Mall address                                      
                })
                console.log(b,'<<<<<<<<<<<<<<',index)
            }
        } 
        console.log('添加结束!!!!!!!!!')



    }



    async backupStoreRecoder2(){
        let data=await storeRecoderModel.find({})
        let data2=await storeRecoderModel2.find({})
        console.log(data.length,'????????????',data2.length)
       /* 
        if(data.length>0){
            for (let index = 0; index < data.length; index++) {
                let res = data[index];
                let b=await storeRecoderModel2.create({
               
                        sourceId: res.sourceId,
                        planId: res.planId,
                        timestamp: res.timestamp,
                        userid: res.userid,//员工的userid
                        month: res.month,
                        date: res.date,
                        planDate: res.planDate,
                        planType: res.planType,
                        storeName: res.storeName,
                        storeCode: res.storeCode,
                        storeType: res.storeType,//示范网吧  模范店等
                        position: res.position,
                        isChuchai: res.isChuchai,//是否出差
                        isListSample: res.isListSample,
                        sampleList: res.sampleList,
                        isListStandard: res.isListStandard,
                        competitor: res.competitor,
                        storeActivity: res.storeActivity,
                        pictureStoreActivity: res.pictureStoreActivity,
                        picture: res.picture,//门头照片
                        isListLetter: res.isListLetter,
                        listLetter: res.listLetter,
                        information: res.information
                                                     
                })
                console.log(b,'<<<<<<<<<<<<<<',index)
            }
        }    */
        console.log('添加结束!!!!!!!!!')



    }



    async backuUserCheckin2(){
        let data=await userCheckInModel.find({})
        let data2=await userCheckInModel2.find({})
        console.log(data.length,'????????????',data2.length)
       /* 
        if(data.length>0){
            for (let index = 0; index < data.length; index++) {
                let e = data[index];
                let b=await userCheckInModel2.create({
                    sourceId:e.sourceId,
                    date_:e.date_,//签到日期
                    month:e.month,//签到月份
                    userId:e.userId,//员工id,
                    checkin_time:e.checkin_time,//员工签到的时间
                    isCheckInNineAm:e.isCheckInNineAm,//是否是上午9:00前签到
                    time_getCheckData:e.time_getCheckData//签到数据 获取的时间   
                                                     
                })
                console.log(b,'<<<<<<<<<<<<<<',index)
            }
        }    */
        console.log('添加结束!!!!!!!!!')



    }



}
let backupService=new BackupService();
export default backupService;